package org.leumasjaffe.initiativeTracker.operator;


import java.io.IOException;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.leumasjaffe.initiativeTracker.entity.Character;
import org.leumasjaffe.initiativeTracker.entity.NPC;
import org.leumasjaffe.initiativeTracker.entity.PC;
import org.w3c.dom.Node;

import org.leumasjaffe.util.io.SerializeString;

@NonNullByDefault
public abstract class CharacterDecoder{
	
	private CharacterDecoder(){}

	@SuppressWarnings("null")
	public static CharacterLinker decode(Node character, String id) {
		String text = "";
		String type = character.getAttributes().getNamedItem("type").getNodeValue();
		Character c = null;
//		System.out.println(character);
//		System.out.println(character.getFirstChild());
//		System.out.println(character.getFirstChild().getFirstChild());
		text = character.getFirstChild().getFirstChild().getNodeValue();
		Object out;
		try {
			out = SerializeString.fromString(text);
			if (type.equals("player")) {
				c = (PC) out;
			}
			else {
				c = (NPC) out;
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		if (c != null)
			return new CharacterLinker(c, id);
		throw new AssertionError("Impossible State");
	}
	
//	@SuppressWarnings("null")
//	public static CharacterLinker decode(NamedNodeMap character, String game) {
//		String name = character.getNamedItem("name").getNodeValue();
//		
//		String type = character.getNamedItem("type").getNodeValue();
//		
//		int hp = Integer.decode(character.getNamedItem("health").getNodeValue());
//		//Contains hp, max hp
//		int main = Integer.decode(character.getNamedItem("base").getNodeValue());
//		// Contains dot,count, initiative, dis
//		
//		int defense = Integer.decode(character.getNamedItem("defense").getNodeValue());
//		// Contains ac,fort,ref,will
//		
//		int status = Integer.decode(character.getNamedItem("status").getNodeValue());
//		//Contains bit string of statuses		
//		
//		int id = Integer.decode(character.getNamedItem("id").getNodeValue());
//		
//		Character c;
//		if (type.equals("player")) {
//			c = new PC();
//			int player = Integer.decode(character.getNamedItem("player").getNodeValue());
//			// Contains temp,max,left
//			((PC) c).setSurgeLeft(player & 0xFF);
//			((PC) c).setSurgeMax((player >> 8) & 0xFF);
//			((PC) c).setTemp((player >>> 16));
//		}
//		else {
//			boolean b = type.equals("friend");
//			c = new NPC(b, (main >> 16) & 0xFF);
//		}
//		
//		c.setId(id);
//		c.setHp((hp) & 0xFFFF);
//		c.setMaxHP((hp >>> 16));
//		c.setDot((main) & 0xFF);
//		c.setWill((defense) & 0xFF);
//		c.setRef((defense >> 8) & 0xFF);
//		c.setFort((defense >> 16) & 0xFF);
//		c.setAc((defense >>> 24));
//		c.readStatus(status);
//		c.setInit((main >> 8) & 0xFF);
//		c.setDis((main >>> 24));
//		c.setName(name);
//		
//		return new CharacterLinker(c, game);	
//	}
	
}